<template>
  <div class="m-import-container">
    <el-button @click="handleImport">导入</el-button>
    <el-dialog title="导入数据" :visible="visible" class="m-dialog m-import-dialog" append-to-body @close="handleCancel">
      <el-row class="clearfix">
        <el-upload
          class="fl mr10"
          :action="importUrl"
          :headers="headers"
          :show-file-list="false"
          :on-change="handleOnChange"
          v-bind="$attrs"
          :disabled="loading"
          :http-request="handleOnProcess"
        >
          <el-button type="primary" :loading="loading">{{ text }}</el-button>
        </el-upload>
        <TemplateDownload class="fl" :url="exportUrl" />
      </el-row>
      <div v-if="status !== ''" class="process-container">
        <div class="file-info clearfix">
          <span class="file-name fl">{{ fileName }}</span>
          <span class="file-size fr">大小：{{ size }}</span>
        </div>
        <div class="progress-box">
          <div class="position-box">
            <el-progress :percentage="percentage" :show-text="false" :status="type" />
            <span class="percentage" :class="{full: percentage === 100}" :style="{left: `${percentage}%`}">{{ `${percentage}%` }}</span>
          </div>
        </div>
      </div>
      <div slot="footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handelOnConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { TemplateDownload, request, fileSizeComputed, getToken, Token } from "@monkey.do/monkey";
import "./import.scss";
export default {
  name: "Import",

  components: {
    TemplateDownload,
  },

  props: {
    url: {
      type: String,
      required: true,
    },
    text: {
      type: String,
      default: "导入数据",
    },
    importUrl: {
      type: String,
      required: true,
    },
    exportUrl: {
      type: String,
      required: true,
    },
    onChange: {
      type: Function,
      default: (file, fileList) => console.log(file, fileList),
    },
  },

  data() {
    return {
      visible: false,
      size: "",
      fileName: "",
      percentage: 0,
      type: "",
      loading: false,
      headers: {
        Authorization: `Bearer ${getToken(Token)}`,
      },
      status: "",
    };
  },

  methods: {
    handleImport() {
      this.visible = true;
    },

    handelOnConfirm() {
      this.visible = false;
    },

    handleCancel() {
      this.visible = false;
      this.reset();
    },

    handleOnChange(file, fileList) {
      if (!this.importUrl) return;
      if (file.status === "ready") {
        this.reset();
        this.status = file.status;
      }
      this.onChange(file, fileList);
    },

    reset() {
      this.type = "";
      this.fileName = "";
      this.percentage = 0;
      this.status = "";
    },

    handleOnProcess(file) {
      const data = new FormData();
      data.append("file", file.file);

      this.fileName = file.file.name;
      this.size = fileSizeComputed(file.file.size);
      this.loading = true;
      request({ url: file.action, data, timeout: 20000, onUploadProgress: (progress) => {
        const percent = progress.loaded / progress.total * 100 | 0;
        this.percentage = percent;
        file.onProgress({ percent });
      } }).then(res => {
        this.loading = false;
        if (res.statudCode !== 600) {
          this.type = "exception";
          this.$message.error(res.message);
        } else {
          this.$message.success("导入成功");
          this.type = "";
        }
        file.onSuccess();
      }).catch((err) => {
        console.log(err);
        this.loading = false;
        this.type = "exception";
        this.$message.error("网络错误!");
      });
    },
  },
};
</script>
